<?php
require('../data/db_config.php');

class db {
	public $conn;
	private $result;
	private $insertid;
	private $affectrows = 1;
	public function __construct() {
		global $db_servername, $db_username, $db_password, $db_database_name, $db_serverport;
		//echo 'db_servername = ' . $db_servername . '<br />';
		$this->conn = new mysqli($db_servername, $db_username, $db_password, $db_database_name, $db_serverport);	
		if (mysqli_connect_errno()) {
    		printf("Connect failed: %s\n", mysqli_connect_error());
    		exit();
		}
		$this->conn->query("set names UTF8");
	}	
	
	// 返回结果集
	// 切记关闭$result, $conn
	function query($query_sql) {
		$this->result = $this->conn->query($query_sql);
		return $this->result;	
	}
	
	function insert_get_id($query_sql) {
		$this->conn->query($query_sql);
		return $this->conn->insert_id;
	}
	
	/* 如果有更新，且更新成功，返回记录id
	 * 如果没有更新，即记录值原来就是如此，返回0
	 * 如果数据库更新失败，返回-1
	 */
	function update_get_id($query_sql, $id) {
		$this->result = $this->conn->query($query_sql);
		$return_id = $id;
		if (!$this->result) {
			$return_id = -1;
		} else {
			if ($this->conn->affected_rows > 0) {
				$return_id = $id;
			} else {
				$return_id = 0;
			}
			return $return_id;
		}
	}

	function update_get_result($query_sql) {
		$return_id = -1;
		$this->result = $this->conn->query($query_sql);
		if ($this->result) {
			if ($this->conn->affected_rows > 0) {
				$return_id = 1;	
			} else {
				$return_id = 0;	
			}
		}	
		return $return_id;
	}
	// 返回stmt
	function prepare($sql) {
		return $this->conn->prepare($sql);
	}
	// 返回结果集的数目
	function query_result_total($query_sql) {
		if ($stmt = $this->conn->prepare($query_sql)) {
			$stmt->execute();
			$stmt->store_result();
			$rows_num = $stmt->num_rows;
			$stmt->close();
			$this->conn->close();
			return $rows_num;
		} else {
			return 0;	
		}
	} 
	
	// 插入数据
	function insert($query_sql) {
		$rs_value;
		$this->query($query_sql);
		if ($this->result > 0) {
			$rs_value = true;
			//echo '数据插入成功 result = ' . $this->result . '<br />';
		} else {
			$rs_value = false;
			//echo '数据插入失败';
		}
		//$this->conn->close();
		return $rs_value;
	}

	function update($query_sql) {
		$rs_value;
		if ($this->result = $this->conn->query($query_sql)) {
			$rs_value = true;
			$this->result->close();
			echo '数据更新成功';
		} else {
			$rs_value = false;
			echo '数据更新失败';
		}
		$this->conn-close();
		return $rs_value;
	}

	function multi_query($sql, $str) {
		if ($this->conn->multi_query($sql)) {
			echo 'mul_qry sc:' . $str . "-ar:" . $this->conn->affected_rows;	
		}	else {
			echo 'mul_qry fl:' . $str . "-ar:" . $this->conn->affected_rows;	
		}
	}

	function result_close() {
		if ($this->result == null) {
			echo 'result is null' . '<br />';
		} else {
			echo 'result = ' . $this->result . '<br />';
		}
		$this->result->close();
	}

	function close() {
		$this->conn->close();	
	}
}

/*function create_db_conn() {
	global $db_servername, $db_serverport, $db_username, $db_password, $db_database_name;
	    
   $conn = new mysqli($db_servername, $db_username, $db_password, $db_database_name, $db_serverport);	
   if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
	}
	
	$conn->query("set names UTF8");
	
	return $conn;
}*/
?>